#include "historymessageitem.h"
#include "model/datacenter.h"

HistoryMessageItem::HistoryMessageItem(const Message &message) : message(message)
{
}

HistoryMessageItem *HistoryMessageItem::makeHistoryMessageItem(const Message &message)
{
    //设置核心属性
    HistoryMessageItem* item = new HistoryMessageItem(message);
    item->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);

    //创建网格布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(10);
    item->setLayout(layout);

    //创建头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(40,40);
    avatarBtn->setIconSize(QSize(40,40));
    avatarBtn->setIcon(message.sender.userIcon);
    avatarBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    avatarBtn->setStyleSheet("QPushButton{border:none;}");

    //创建昵称 + 时间文本
    QLabel* nameLabel = new QLabel();
    nameLabel->setFixedHeight(20);
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameLabel->setText(message.sender.userName + "|" + message.sendTime);

    //创建消息内容部分
    QWidget* contentWidget = nullptr;
    switch(message.messageType)
    {
        case MessageType::TEXT_TYPE:
            {
                QLabel* label = new QLabel();
                label->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
                label->setWordWrap(true);
                label->adjustSize();
                label->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
                label->setText(QString(message.messageContent));
                contentWidget = label;
            }
            break;
        case MessageType::FILE_TYPE:
            {
                contentWidget = new FileLabel(message.fileId,message.fileName);
            }
            break;
        case MessageType::SPEECH_TYPE:
            {
                contentWidget = new SpeechLabel(message.fileId);
            }
            break;
        case MessageType::IMAGE_TYPE:
            {
                contentWidget = new ImageButton(message.fileId,message.messageContent);
            }
            break;

        default:
            LOG() << "HistoryMessageItem *HistoryMessageItem::makeHistoryMessageItem(const Message &message) 执行失败: 未知消息类型";
            break;
    }

    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(contentWidget, 1, 1, 5, 1);
    return item;
}

ImageButton::ImageButton(const QString &fileId, const QByteArray &content)
{
    this->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    this->setStyleSheet("QPushButton { border:none; }");

    if(!content.isEmpty())
    {
        this->updateUI(fileId,content);
    }
    else
    {
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        connect(dataCenter,&model::DataCenter::getSingleFileDone,this,&ImageButton::updateUI);
        dataCenter->getSingleFileAsync(fileId);
    }
}

void ImageButton::updateUI(const QString &fileId, const QByteArray &content)
{
    if(fileId != this->fileId)
    {
        return;
    }
    QImage image;
    image.loadFromData(content);
    QPixmap pixmap = QPixmap::fromImage(image);

    if(pixmap.width() >= 300)
    {
        pixmap = pixmap.scaledToWidth(300);
    }

    this->resize(pixmap.size());
    this->setIconSize(pixmap.size());
    this->setIcon(QIcon(pixmap));

}

FileLabel::FileLabel(const QString &fileId, const QString &fileName)
{
    this->setText("[文件]" + fileName);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    this->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    this->setWordWrap(true);
    this->adjustSize();

    model::DataCenter* dataCenter = model::DataCenter::getInstance();;
    connect(dataCenter,&model::DataCenter::getSingleFileDone,this,&FileLabel::getContentDone);
    dataCenter->getSingleFileAsync(fileId);
}

void FileLabel::getContentDone(const QString& fileId, const QByteArray& content)
{
    if(this->fileId != fileId)  return;

    this->content = content;
    loadContentDone = true;
}

void FileLabel::mousePressEvent(QMouseEvent *event)
{
    if(!this->loadContentDone)
    {
        Toast::showMessage("数据尚未加载完毕, 请稍后再试");
        return;
    }

    QString filePath = QFileDialog::getSaveFileName(this,"另存为",QDir::homePath(),"files (*)");

    if(filePath.isEmpty())
    {
        LOG() << "用户取消了文件选择!";
        return;
    }

    writeByteArrayToFile(filePath,this->content);
}

SpeechLabel::SpeechLabel(const QString &fileId)
{
    this->setText("[语音]");
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    this->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    this->setWordWrap(true);
    this->adjustSize();

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::getSingleFileDone,this,&SpeechLabel::getContentDone);
    dataCenter->getSingleFileAsync(fileId);
}

void SpeechLabel::getContentDone(const QString &fileId, const QByteArray &content)
{
    if(this->fileId != fileId)  return;

    this->content = content;
    this->loadContentDone = true;
}

void SpeechLabel::mousePressEvent(QMouseEvent *event)
{
    if(!this->loadContentDone)
    {
        Toast::showMessage("数据尚未加载完毕, 请稍后再试");
        return;
    }

    SoundRecorder* soundRecorder = SoundRecorder::getInstance();

    soundRecorder->startPlay(this->content);
}
